package game;

import game.Enemty.*;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.Timer;
import java.util.List;

public class MainPanel extends JPanel implements KeyListener,Runnable{
    Background b = new Background();
    Catt cat=new Catt();
    Thread t = new Thread(this);
    List<Obstacle> obstacles=new ArrayList<>();
    List<Award> awards=new ArrayList<>();
    List<Monster> monsters = new ArrayList<Monster>();
    List<Monster2> monsters2 = new ArrayList<Monster2>();
    boolean[] hpReale = {true, true, true};

    public MainPanel() {
        t.start();
        addKeyListener(this);//添加键盘监听时间
        setFocusable(true);//获取焦点
        obstacles=b.obstacles;
        awards=b.awards;
        monsters.add(new Monster(600,300,30,30));
        monsters.add(new Monster(800,300,30,30));
        monsters.add(new Monster(1000,300,30,30));
        initTimer();
    }


    @Override
    public void paint(Graphics g) {
        super.paint(g);
        //打印
        printBackgourd(g);
        //打印猫
        printCat(g);
        //打印award
        paintAward(g);
        //打印障碍物
        printObstacle(g);
        //打印怪物
        paintEnemy(g);
        //打印状态栏
        paintStatusBar(g);
        //打印旗帜
        paintFlag(g);
    }

    //打印旗帜
    private void paintFlag(Graphics g) {
        g.drawImage(b.flag, b.flagX, b.flagY, 30,100,null);
    }


    //打印猫
    private void printCat(Graphics g) {
        g.drawImage(cat.img,cat.x,cat.y,40,50,this);
    }

    //背景移动
    public void printBackgourd(Graphics g) {
        g.drawImage(b.background, b.x, b.y,b.background.getWidth(null),b.background.getHeight(null),null);
    }

    //键盘监听
    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        if(e.getKeyCode() == KeyEvent.VK_RIGHT) {
            cat.rightMove();
        }
        if(e.getKeyCode() == KeyEvent.VK_LEFT) {
            cat.leftMove();
        }
        if(e.getKeyCode() == KeyEvent.VK_UP) {
            cat.jump();
        }
        if(e.getKeyCode() == KeyEvent.VK_DOWN) {
            cat.fall();
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
        //想左停止
        if (e.getKeyCode() == KeyEvent.VK_LEFT) {
            cat.leftStop();
        }
        //向右停止
        if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
            cat.rightStop();
        }
    }

    @Override
    public void run() {
        while (true){
            //人物运动
            b.move(cat);
            //数据投同步
            cat.sync(obstacles,awards,monsters,hpReale);
            //怪物逻辑
            enemyAction();

            isTouchFlag();

            repaint();
            isDead();//死亡重开进程
            monsterOb();//怪物障碍物判断
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }

    private void monsterOb() {
        for (int i = 0; i < monsters.size(); i++) {
            for (int j = 0; j < obstacles.size(); j++) {
                monsters.get(i).checkMonsterOb(new Rectangle(obstacles.get(j).x, obstacles.get(j).y, obstacles.get(j).width, obstacles.get(j).height));
            }
        }
    }

    private void isDead() {
        if(cat.dead()){
            JDialog dialog = new JDialog();
            dialog.setTitle("游戏结束");
            dialog.setSize(300, 200);
            dialog.setLocationRelativeTo(null);
            dialog.setModal(true);
            dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            JLabel label = new JLabel("嘎了!", JLabel.CENTER);
            dialog.add(label);
            dialog.pack();
            dialog.setVisible(true);

            // 重新开始游戏
            cat = new Catt(); // 重新初始化角色
            b = new Background(); // 重新初始化背景和障碍物等
            obstacles = b.obstacles;
            awards = b.awards;
            monsters.clear();
            monsters.add(new Monster(600, 300, 30, 30));
            monsters.add(new Monster(800, 300, 30, 30));
            hpReale = new boolean[]{true, true, true}; // 重置血量状态
            b.end = false; // 重置关卡完成状态
            cat.restart = false; // 重置角色复活状态
        }
//    }        if(cat.dead()){
//            JDialog dialog = new JDialog(); // 创建一个 JDialog 实例
//            dialog.setTitle("My Dialog"); // 设置对话框标题
//            dialog.setSize(300, 200); // 设置对话框大小
//            dialog.setLocationRelativeTo(null); // 居中显示对话框
//            dialog.setModal(true); // 设置对话框为模态
//            dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); // 设置关闭操作
//            // 可以添加组件到对话框中，例如一个 JLabel
//            JLabel label = new JLabel("嘎了!", JLabel.CENTER);
//            dialog.add(label);
//            dialog.pack(); // 根据组件自动调整大小
//            dialog.setVisible(true); // 显示对话框
//            Runtime.getRuntime().exit(0);
//        }

    }


    //怪物部分

    public void paintEnemy(Graphics g){
        for(int i=0;i<monsters.size();i++){
            Monster monster = monsters.get(i);
            g.drawImage(monster.enemyImg,monster.x,monster.y,monster.width,monster.height,null);
        }
    }

    public void enemyAction(){
        for (int i = 0; i < monsters.size(); i++) {
            Monster monster = monsters.get(i);
            monster.moveAndStep();
        }

    }

    public void paintEnemy2(Graphics g){
        for(int i=0;i<monsters2.size();i++){
            Monster2 monster2 = monsters2.get(i);
            g.drawImage(monster2.enemyImg2,monster2.x,100,50,50,null);
        }
    }

    public void enemyAction2(){
        for (int i = 0; i < monsters2.size(); i++) {
            Monster2 monster2 = monsters2.get(i);
            monster2.moveAndStep2();
        }

    }












    //状态栏
    Image img1 = new ImageIcon("img/background/font_top1.png").getImage();
    Image img2 = new ImageIcon("img/background/font_top2.png").getImage();
    Image img3 = new ImageIcon("img/background/font_top3.png").getImage();
    // 血量图片路径
    Image imgHp = new ImageIcon("img/background/hp.png").getImage();
    Image imgHpNull=new ImageIcon("img/background/hpNull.png").getImage(); // 空血图片
    // 计时器相关变量
    private int seconds = 0;
    private javax.swing.Timer gameTimer;
    private Font timeFont = new Font("Arial", Font.BOLD, 28);

    private void initTimer() {
        // 创建每秒触发一次的计时器
        gameTimer = new javax.swing.Timer(1000, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                seconds++;
                repaint(); // 更新时间显示
            }
        });
        gameTimer.start(); // 自动开始计时
    }
    public class StatusBar {
        static int score;
        public StatusBar(){
            score = 100;
        }
    }
    public void paintStatusBar(Graphics g){
        g.setColor(Color.white);//数字颜色色
        Font f = new Font("Arial", Font.BOLD, 28);//Font的三个参数分别表示字体，加粗，大小
        g.setFont(f);
        // 绘制状态栏背景
        g.drawImage(img2, 0, 5, null);
        g.drawImage(img1, 500, 0, null);
        g.drawImage(img3, 250, 3, null);
        //血量显示

        int HpXStart = 5;
        for (int i = 0; i < 3; i++) {
            if (hpReale[i]) {
                g.drawImage(imgHp, HpXStart, 50, null); // 绘制满血图标
            } else {
                g.drawImage(imgHpNull, HpXStart, 50, null); // 绘制空血图标
            }
            HpXStart += 50; // 血量间隔50像素
        }
        // 绘制关卡和分数
        g.drawString("" + (StatusBar.score / 1000 + 1), 180, 33); // 关卡
        g.drawString("" + cat.coin, 700, 33); // 分数
        // 绘制时间
        g.setFont(timeFont);
        g.setColor(Color.WHITE); // 设置时间文字颜色
        String timeStr = formatTime(seconds);
        g.drawString(timeStr, 400, 33); // 时间显示位置

    }
    // 格式化时间为MM:SS
    private String formatTime(int totalSeconds) {
        int minutes = (totalSeconds % 3600) / 60;
        int secs = totalSeconds % 60;
        return String.format("%02d:%02d", minutes, secs);
    }



    //旗帜
    public void isTouchFlag(){
        if(b.end){
            JDialog dialog = new JDialog(); // 创建一个 JDialog 实例
            dialog.setTitle("My Dialog"); // 设置对话框标题
            dialog.setSize(300, 200); // 设置对话框大小
            dialog.setLocationRelativeTo(null); // 居中显示对话框
            dialog.setModal(true); // 设置对话框为模态
            dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); // 设置关闭操作
            // 可以添加组件到对话框中，例如一个 JLabel
            JLabel label = new JLabel("结束!", JLabel.CENTER);
            dialog.add(label);
            dialog.pack(); // 根据组件自动调整大小
            dialog.setVisible(true); // 显示对话框
            System.exit(0);
        }
    }








    //障碍物
    private void printObstacle(Graphics g) {
        for (int i = 0; i < obstacles.size(); i++) {
            Obstacle obstacle = obstacles.get(i);
            g.drawImage(obstacle.image,obstacle.x,obstacle.y,obstacle.width,obstacle.height,null);
        }
    }


    //奖励
    public void paintAward(Graphics g){
        for (int i = 0; i < awards.size(); i++) {
            Award award=awards.get(i);
            g.drawImage(new ImageIcon("img/award/1.gif").getImage(),award.x,award.y,award.width,award.height,null);
        }
    }


    //人物碰到奖励


}
